package cn.com.mjsoft.framework.persistence.core.support.transform;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.com.mjsoft.framework.persistence.core.JdbcInstrument;
import cn.com.mjsoft.framework.persistence.core.RowTransform;
import cn.com.mjsoft.framework.persistence.core.support.MapValueCallback;

/**
 * 将每一行数据的每个值转换为name-value的形式
 * 
 * @author mjsoft
 * 
 */
public class MapValueRowTransform implements RowTransform
{
    private MapValueCallback callback = null;

    public MapValueRowTransform()
    {
    }

    public MapValueRowTransform( MapValueCallback callback )
    {
        this.callback = callback;
    }

    public Object convertRow( ResultSet rs, int rowNum ) throws SQLException
    {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Map<String, Object> valuesMap = new LinkedHashMap<String, Object>( columnCount );
        for ( int i = 1; i <= columnCount; i++ )
        {
            String key = rsmd.getColumnLabel( i );

            Object obj = JdbcInstrument.getResultSetValue( rs, i );

            valuesMap.put( key, obj );
        }

        if( callback != null )
        {
            callback.transformVlaue( valuesMap );
        }

        return valuesMap;
    }
}
